import axios from 'axios';
import qs from 'qs';
// create an axios instance
const service = axios.create({
    // baseURL: process.env.BASE_API, // api的base_url
    // timeout: 5000, // request timeout
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    },
});

// request interceptor
service.interceptors.request.use((config) => {
    const cfg = config;
    cfg.method = 'post';

    cfg.data = qs.stringify(cfg.data);
    return cfg;
}, (error) => {
    // Do something with request error
    console.log(error); // for debug
    Promise.reject(error);
});

// respone interceptor
service.interceptors.response.use((response) => {
        /**
         * 下面的注释为通过response自定义code来标示請求状态，当code返回如下情况为权限有问题，登出并返回到登录页
         * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
         */
        const res = response.data;
        return res;
    },

    (error) => {
        console.log(`err${error}`);// for debug

            alert(error.message)

        return Promise.reject(error);
    });

service.all = axios.all;

export default service;
